package com.aoratec.demo;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class TouchActivity extends Activity{
	
	private LinearLayout main = null;
	private Button A = null;
	private Button B = null;
	private Button C = null;
	private Button D = null;
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.touch);
		main = (LinearLayout) findViewById(R.id.ABCD);
		A = (Button) findViewById(R.id.A);
		B = (Button) findViewById(R.id.B);
		C = (Button) findViewById(R.id.C);
		D = (Button) findViewById(R.id.D);
		final Rect aRect = new Rect();
		final Rect bRect = new Rect();
		final Rect cRect = new Rect();
		final Rect dRect = new Rect();
	/*	aRect.left = A.getLeft();
		aRect.right = A.getRight();
		aRect.top = A.getTop();
		aRect.bottom = A.getBottom();*/
		A.setFocusable(false);
		A.setClickable(false);
		B.setFocusable(false);
		B.setClickable(false);
		D.setFocusable(false);
		D.setClickable(false);
		C.setFocusable(false);
		C.setClickable(false);
		
	main.setOnTouchListener(new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_DOWN){
			//	B.getLocalVisibleRect(aRect);
				//int []location = new int[2];
				//B.getLocationInWindow(location);
			//	B.getLocationOnScreen(location);
				aRect.set(A.getLeft(), A.getTop(), A.getRight(), A.getBottom());
				dRect.set(D.getLeft(), D.getTop(), D.getRight(), D.getBottom());
				bRect.set(B.getLeft(), B.getTop(), B.getRight(), B.getBottom());
				cRect.set(C.getLeft(), C.getTop(), C.getRight(), C.getBottom());
			//	aRect.left = location[0];
			//	aRect.left = location[1];
				Log.e("", "aRect.left ="+aRect.left+"aRect.right= "+aRect.right+"aRect.top= "+aRect.top+"aRect.bottom= "+aRect.bottom);
			//	Log.e("", "location[0] ="+location[0]+"location[1]= "+location[1]);
			}else if(event.getAction()==MotionEvent.ACTION_MOVE){
				int x = (int) event.getX();
				int y = (int) event.getY();
				//Log.e("aaaaaaaaa", "x ="+x+"y ="+y);
				if(aRect.contains(x, y)){
					A.setText("touch A");
					Log.e("", "AAAAAAAAAAAAAAAAAA");
				}
				if(bRect.contains(x, y)){
					B.setText("touch B");
					Log.e("", "BBBBBBBBBBBBBBBBBBBB");
				}
				if(cRect.contains(x, y)){
					C.setText("touch C");
					Log.e("", " CCCCCCCCCCCCCCCCC");
				}
				if(dRect.contains(x, y)){
					D.setText("touch D");
					Log.e("", " DDDDDDDDDDDDDDDDD");
				}
			}else if(event.getAction()==MotionEvent.ACTION_UP){
					A.setText("A");
					D.setText("D");
					C.setText("C");
					B.setText("B");
		
			}
			return true;
		}
	});
	
	}
	
	
	
	

}
